package com.masterclient.app.businessmodule.usermodule.login.invite;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.middlelib.eventbus.EventBusManager;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.usermodule.login.beans.AuthBindData;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviteInfoData;
import com.masterclient.app.businessmodule.usermodule.login.beans.InviterPopModel;
import com.masterclient.app.businessmodule.usermodule.login.beans.LoginSuccessData;
import com.masterclient.app.businessmodule.usermodule.login.dialog.InviteUserDialog;
import com.masterclient.app.businessmodule.usermodule.login.invite.presenter.BindInvitePresenter;
import com.masterclient.app.businessmodule.usermodule.login.invite.presenter.IBindInviteView;

import butterknife.BindView;

public class BindInviteActivity extends FCBusinessActivity<BindInvitePresenter> implements IBindInviteView {
    private InviteUserDialog dialog;
    public final static int REQUEST_CODE = 900;
    public final static int BIND_SUC_CODE = 1900;

    @BindView(R.id.et_pwd)
    EditText et_pwd;

    @BindView(R.id.tv_verify)
    TextView tv_verify;

    private String rewardId = "";
    private LoginSuccessData loginSuccessData;
    public String phone, code, auth_type, union_id, nick_name, headimgurl;
    InviterPopModel inviter_pop;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_bind_invite;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setLineHidden(true);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        phone = getBundleString("phone");
        auth_type = getBundleString("auth_type");
        union_id = getBundleString("union_id");
        nick_name = getBundleString("nick_name");
        headimgurl = getBundleString("headimgurl");
        inviter_pop = (InviterPopModel) getIntent().getSerializableExtra("inviter_pop");
        loginSuccessData = (LoginSuccessData) getIntent().getSerializableExtra("lg_data");

        if (loginSuccessData.getInviter_code_select_pop() != null && loginSuccessData.getInviter_code_select_pop().bindInviterCode != null) {
            rewardId = loginSuccessData.getInviter_code_select_pop().bindInviterCode.rewardId;
        }
        tv_verify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.getInviteInfo(et_pwd.getText().toString());
            }
        });


        et_pwd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

                if (charSequence == null || charSequence.length() == 0) {
                    et_pwd.setTypeface(Typeface.DEFAULT);
                    return;
                }

                if (charSequence.length() > 0) {
                    Utils.setDin(et_pwd, mContext);
                    tv_verify.setClickable(true);
                    tv_verify.setBackgroundResource(R.drawable.login_able_click_bkg);
                    tv_verify.setTextColor(0xffffffff);
                } else {
                    tv_verify.setClickable(false);
                    tv_verify.setBackgroundResource(R.drawable.login_unable_click_bkg);
                    tv_verify.setTextColor(0xffffffff);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }


    @Override
    public void showInviteInfo(InviteInfoData data) {
        rewardId = "";
        if (dialog!=null && dialog.isShowing()) {
            return;
        }
        if (dialog == null) {
            dialog = new InviteUserDialog(getActivity(), new InviteUserDialog.OnChooseListener() {
                @Override
                public void cancel() {
                    dialog.dismiss();
                }

                @Override
                public void confirm(String inviteCode) {
                    dialog.dismiss();
                    if (inviteCode == null || inviteCode.length() == 0) {
                        inviteCode = et_pwd.getText().toString();
                    }
                    baseShowCommitLoading("");

                    mPresenter.authBind(auth_type,  phone.toString().trim().replace(" ", ""), union_id, inviteCode, Config.uid, nick_name, headimgurl,
                            loginSuccessData.getNonce(), SPUtils.getInstance().getString(Config.CUSTOMERID_TEMP), rewardId);
                }
            });
        }
        dialog.show();
        dialog.setData(loginSuccessData);
        dialog.refreshData(data);
    }

    @Override
    public void showInviteInfoFail(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void authBindSuccess(String code, AuthBindData data, String msg, String inviteCode) {
        UserDataManager.getInstance().updateInviteCode(inviteCode);
        if ("30000".equals(code)) {
            rewardId = data.getInviteInfo().getReward_id();
            dialog.refreshInviteInfo(data.getInviteInfo(), data.getNotice());
        } else {
            if (dialog != null) {
                dialog.dismiss();
            }
            if (data != null) {
                if (!TextUtils.isEmpty(data.getCustomer_id())) {
                    SPUtils.getInstance().put(Config.CUSTOMERID, data.getCustomer_id());
                } else {
                    SPUtils.getInstance().put(Config.CUSTOMERID, SPUtils.getInstance().getString(Config.CUSTOMERID_TEMP));
                }
                if (!TextUtils.isEmpty(data.getToken())) {
                    SPUtils.getInstance().put(Config.CUSTOMER_TOKEN, data.getToken());
                } else {
                    SPUtils.getInstance().put(Config.CUSTOMER_TOKEN, SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN_TEMP));
                }
            }
            EventBusManager.post(EventBusManager.createLoginSuccessEvent());
            EventBusManager.post(EventBusManager.createHomeRefreshEvent());

            getActivity().setResult(BIND_SUC_CODE);
            getActivity().finish();
        }
        baseHiddenCommitLoading();
        showToast(msg);
    }

    @Override
    public void authBindFailded(String msg) {
        baseHiddenCommitLoading();
        showToast(msg);
    }
}
